<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Samecity;
use App\Http\Model\User;
use Illuminate\Http\Request;

class SamecityServices extends CommonServices
{
    protected $samecityModel;
    protected $userModel;

    public function __construct()
    {
        $this->samecityModel = new Samecity();
        $this->userModel = new User();
    }

    /**
     * 创建
     * */
    public function createSamecity(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $content = $request->post("content");

            if (empty($content)) {
                $code = Hint::LOGIN_OUT;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            //获取用户信息
            $currentUser = $this->userModel->get_current_user();
            if (empty($currentUser)) {
                $code = Hint::PARAM_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $requestData['content'] = $content;
            $requestData['user_id'] = $currentUser['user_id'] ?? 0;
            $requestData['created_at'] = time();
            // 创建
            $ret = $this->samecityModel->create($requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg =  Hint::getErrCodeMsg($code);
                break;
            }

            $data['id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }
}